import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JTextArea;

public class VueRechercheMotCle extends Vue {

	private JPanel contentPane;
	
	private JList list;
	private HashSet<Ouvrage> listO;
	private HashSet<Article> listA;
	private Vector<String> theme;
	private Vector<String> ouvrages;
	private String decoup;
	private JTextArea textAreaOuv;
	private JTextArea textAreaArt;
	private JScrollPane scrollPane_1;
	private JScrollPane scrollPaneArt;
	
	public VueRechercheMotCle(Controleur c, HashSet<MotCle> mc) {
		
		super(c);
		setTitle("Rechercher par theme");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 430, 515);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Theme");
		lblNewLabel.setBounds(50, 76, 61, 15);
		contentPane.add(lblNewLabel);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBounds(129, 41, 216, 79);
		contentPane.add(scrollPane);
		theme = new Vector<String>(); 
		for(MotCle t:mc){
			theme.add(t.getMotCle());
		}
		
		list = new JList(theme);
		scrollPane.setViewportView(list);
		
		JButton btnRechercher = new JButton("Rechercher");
		btnRechercher.setBounds(257, 155, 107, 25);
		btnRechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String selectTheme;
				Object val;
				val=list.getSelectedValue();
				if(val!=null){
					selectTheme=val.toString();
					listO=getControleur().RechercheParMotCleOuv(selectTheme);
					listA=getControleur().RechercheParMotCleArt(selectTheme);
					
					textAreaOuv = new JTextArea();
					scrollPane_1.setViewportView(textAreaOuv);
					textAreaArt= new JTextArea();
					scrollPaneArt.setViewportView(textAreaArt);
					//ouvrage
					if(listO.isEmpty()){
						Message dialog = new Message("Il n'existe pas d'ouvrage avec ce theme");
						dialog.setVisible(true);
					}
					else{
						for(Ouvrage ouv:listO){
							textAreaOuv.append("Isbn :" + String.valueOf(ouv.getIdentifiant()) + " Titre : " + String.valueOf(ouv.getTitre()) + "\n");
						}
				}
					
					if(listA.isEmpty()){
						Message dialog = new Message("Il n'existe pas d'article avec se theme");
						dialog.setVisible(true);
					}
					else{
						for(Article art:listA){
							textAreaArt.append("Issn :" + String.valueOf(art.getParution().getPeriodique().getIssn())+" Num parution :" + String.valueOf(art.getParution().getNumPar())  + " titre : " + String.valueOf(art.getTitre()) + "\n");
						}
					}
				}
			
					
				else{
					Message dialog = new Message("Veuillez choisir un theme (cliquez dessus) ");
					dialog.setVisible(true);
				}
			}

		});
		contentPane.add(btnRechercher);
		btnRechercher.setEnabled(true);
		
		JLabel lblOuvrage = new JLabel("Ouvrage");
		lblOuvrage.setBounds(50, 257, 61, 15);
		contentPane.add(lblOuvrage);
		
		ouvrages= new Vector<String>();
		
		JLabel lblArticl = new JLabel("Article");
		lblArticl.setBounds(50, 368, 61, 15);
		contentPane.add(lblArticl);

		JScrollPane scrollPaneArt = new JScrollPane();
		scrollPaneArt.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		scrollPaneArt = new JScrollPane();

		scrollPaneArt.setBounds(129, 336, 216, 79);
		contentPane.add(scrollPaneArt);

		textAreaArt = new JTextArea();
		scrollPaneArt.setViewportView(textAreaArt);

		
		JButton btnQuitter = new JButton("Quitter");
		btnQuitter.setBounds(162, 438, 107, 25);
		btnQuitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueRechercheMotCle.this);}
		});
		contentPane.add(btnQuitter);
		btnQuitter.setEnabled(true);
		
		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(129, 211, 216, 79);
		contentPane.add(scrollPane_1);
		

	}
	
	
	
	
	
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {

			break;
			}

		}
	}
}
